package com.debby.test.fish;

import android.app.ActivityGroup;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.debby.xpsoft.EncryptionDecryption;

public class HomeActivity extends ActivityGroup{
	

	float a = 0;
	float b = 0;
	int screenWidth;
	int screenHeight;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub 
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        DisplayMetrics dm;//屏幕分辨率容器
        dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;
        a = screenWidth/800f*140;
        b = screenHeight/480f*310;
		setContentView(new MyView(this));
		startService(new Intent(this,VersionUpdateService.class));
//		ImageView iv = new ImageView(getApplicationContext());
//		iv.setBackgroundResource(R.drawable.enter);
//		LinearLayout rl = (LinearLayout) findViewById(R.id.mainlayout);
//		LinearLayout lla = new LinearLayout(getApplicationContext());
//		lla.addView(iv);
//		LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
//		ll.leftMargin = (int)a;
//		ll.topMargin = (int)b;
//		lla.setLayoutParams(ll);
		
		
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			Rect rect = new Rect((int)a,(int)b,(int)a+enterW,(int)b+enterH);
			if(rect.contains((int)event.getX(),(int)event.getY())){
				if(hasAPP()){
					startActivity(new Intent(this,FishfreeActivity.class));
					finish();
				}else{
					Toast.makeText(getApplicationContext(), "先要安装捕鱼之海底捞哦", Toast.LENGTH_LONG).show();
				}
			}
			break;

		default:
			break;
		}
		return super.onTouchEvent(event);
	}
	int enterH = 0;
	int enterW = 0;
	private class MyView extends View{

		public MyView(Context context) {
			super(context);
		}
		
		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			//定义预转换成的图片的宽度和高度
	        int newWidth = 800;
	        int newHeight = 480;
	        
	        //计算缩放率，新尺寸除原始尺寸
	        float scaleWidth = ((float) screenWidth) / newWidth;
	        float scaleHeight = ((float) screenHeight) / newHeight;
	        // 创建操作图片用的matrix对象
	        Matrix matrix = new Matrix();
	        
	        // 缩放图片动作
	        matrix.postScale(scaleWidth, scaleHeight);
	        Bitmap enterb = BitmapFactory.decodeResource(getResources(), R.drawable.enter);
	        enterH = enterb.getHeight();
	        enterW = enterb.getWidth();
			canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.splash), matrix, null);
			canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.enter), a, b, null);
		}
		
	}
	
	
	public void start(View view){
		if(hasAPP()){
			startActivity(new Intent(this,FishfreeActivity.class));
		}else{
			Toast.makeText(getApplicationContext(), "先要安装捕鱼之海底捞哦", Toast.LENGTH_LONG).show();
		}
	}
	
	public boolean hasAPP(){
		PackageInfo packageInfo;

	      try {
	          packageInfo = this.getPackageManager().getPackageInfo("com.sg.android.fish", 0);

	      } catch (NameNotFoundException e) {
	          packageInfo = null;
	          e.printStackTrace();
	      }
	      if(packageInfo ==null){
	          return false;
	      }else{
	    	  return true;
	      }
	}
}
